// pages/setfood/setfood.js
var util=require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stime: '',
    etime: '',
    files: [],
    imgsrc:'',
    shop:{},
    shoplogo:'/img/me.jpg',

    region: ['北京市', '北京市', '朝阳区'],
    customItem: '请选择',
    showDialog: false,
    markers: [{
      iconPath: "/img/location.png",
      id: 0,
      latitude: 38.87229,
      longitude: 115.4745211,
      width: 50,
      height: 50
    }],

    longitude: 115.474520,
    latitude: 38.879994,
    allHeight: 1000,

    address:{}
  },

  onLoad: function (options){
    var that=this;
    var that = this;
    wx.getSystemInfo({
      success: function (res, rect) {
        that.setData({
          allHeight: res.windowWidth
        })
      }
    })
    that.getCurrentLocation();
      util.fetchGet(
        util.url.shop_shop_getshop,
        function (ret) {
          var myshop = ret.data;
          var sevicetime = myshop.servicetime;
          if (myshop.address){
            var region = [myshop.address.province, myshop.address.city, myshop.address.area]
            that.setData({
              region: region,
              address: myshop.address,
              longitude: myshop.address.location.x,
              latitude:myshop.address.location.y,
            })
          }        
          that.setData({
            shop: myshop,            
            shoplogo:wx.getStorageSync('imghost')+myshop.pic,            
        })
        }
      )   
  },

  bindTimeChange1: function (e) {
    this.setData({
      stime: e.detail.value
    })
  },
  bindTimeChange2: function (e) {
    this.setData({
      etime: e.detail.value
    })
  },
  bindconcat: function (e) {
    this.setData({
      'shop.contact': e.detail.value
    })
  },
  bindphone: function (e) {
    this.setData({
      'shop.phone': e.detail.value
    })
  },
  bindshopname: function (e) {
    this.setData({
      'shop.name': e.detail.value
    })
  },
  bindaddress: function (e) {
    this.setData({
      'address.address': e.detail.value
    })
  },
  bindsubmit:function(){
    var that=this;
    var region=this.data.region;
    var oldaddress = this.data.address;
    var address = {};
    address.mapAddress=oldaddress.mapAddress;
    address.address=oldaddress.address;
    var shop={};
//    address.province = region[0];
//    address.city = region[1];
//    address.area = region[2];
    var oldshop=that.data.shop;
    address.loc = [this.data.longitude, this.data.latitude];
    shop.address=address;
    shop.name=oldshop.name;
    shop.phone = oldshop.phone;
    shop.contact = oldshop.contact;
    shop.pic = oldshop.pic;
    if (typeof oldshop.specialty == "string"){
      shop.specialty = oldshop.specialty.split(',');
    }else{
      shop.specialty = oldshop.specialty;
    }
    
    
    shop.weiuser=wx.getStorageSync('weiuser').id 
    
    util.fetchPost(
      util.url.shop_shop_addshop,
      //'myshop/add?',
      shop,
       function(ret){
         console.log(ret);
         wx.navigateBack({})
       }
    )
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: wx.getStorageSync('host') +'/file/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'user': 'test' 
          },
          success(res) {
            const data =JSON.parse( res.data );
            //do something
            that.setData({
              'shop.pic': data.data,
              'shoplogo': wx.getStorageSync('host') + '/file/image/' + data.data
            })
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  bindRegionChange: function (e) {
    var region=e.detail.value;

    this.setData({
      region: region,
    })
  },
  getCurrentLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        var marker=that.data.markers;
        marker[0].longitude = longitude;
        marker[0].latitude = latitude;
        that.setData({
          markers: marker,
          longitude: longitude,
          latitude: latitude
        })
      }
    })
  },
  bindMark: function (e) {
    console.log(e.detail.longitude);
    console.log(e.detail.latitude);
    var address=this.data.address;
    var marks = this.data.markers;
    address.loc = [e.detail.longitude, e.detail.latitude];
    
    var mark = {
      iconPath: "/img/location.png",
      id: marks.length,
      latitude: e.detail.latitude,
      longitude: e.detail.longitude,
      width: 50,
      height: 50,
      label: {
        content: this.data.shop.name,
        color: '#1aad19',
        textAlign: "left",
        borderColor: "#000"
      }
    }
    marks[0] = mark;
    //marks.push(mark);
    this.setData({
      markers: marks,
      address:address
    })
  },

  openDialog: function () {
    this.setData({
      istrue: true
    })
  },
  closeDialog: function () {
    this.setData({
      istrue: false
    })
  },
  bindTextArea:function(e){
    var value=e.detail.value;
    value = value.replace("，",",");
    value = value.replace(" ", "");
    this.setData({
      'shop.specialty':value
    })
  },
  getAddress: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        //if (res.address.length > 10) {
        //  res.address = res.address.substr(0, 10) + '...'
        //}
        that.setData({
          'address.mapAddress': res.address,
          latitude: res.latitude,
          longitude:res.longitude
        })
      }, 
    })
  },
})